package com.project.collectAPI.comm;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;

public class FileUtils {
    /*
    根据文件所在路径下载文件
     */
    public static void download(HttpServletResponse response, String filePath) {
        File file = new File(filePath);
        // 取得文件名。
        String fileName = file.getName();
        System.out.println(fileName+"1111111111111111111111");
        InputStream fis = null;
        try {
//        URL url = new URL(filePath);
//        InputStream fis = url.openStream();
            fis = new FileInputStream(file);
            response.reset();
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/x-msdownload");// 设置强制下载不打开
            response.addHeader("Content-Disposition",
                    "attachment;filename=" + new String(fileName.getBytes("utf-8"), "iso8859-1"));
//            response.setHeader("Content-Length", String.valueOf(file.length()));

            byte[] b = new byte[1024];
            int len;
            while ((len = fis.read(b)) != -1) {
                response.getOutputStream().write(b, 0, len);
            }
            response.flushBuffer();
            fis.close();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception{

        File targetFile = new File(filePath);
        if(!targetFile.exists()){
            targetFile.mkdirs();
        }
        FileOutputStream out = new FileOutputStream(filePath+fileName);
        out.write(file);
        out.flush();
        out.close();
    }

    public static String downloadFile(HttpServletResponse response, String path) {
        if (path != null) {
            //设置文件路径
            File file = new File(path);
            String fileName = file.getName();
            if (file.exists()) {
                response.setContentType("application/force-download");// 设置强制下载不打开
                response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    return "下载成功";
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return "下载失败";
    }


    /**
     * 删除本地服务器图片
     * @param filePath     文件路径   去除https后 剩余的路径  images/firstclassimgs/b9649186-1643-4818-b019-beb7f3178eb8.png
     */
    public static String deleteLocalServerImage(String fileName){
        File file = new File("src/main/resources/static/images/screenShot/imgFile/"+fileName);
        try {
            boolean flag = file.delete(); // 删除照片
            if (flag) {
                return "删除成功";
            } else {
                return "删除失败！";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "图片不存在或其他异常！";
        }
    }



    /**
     * 获取图片名称
     * @param filePath
     */
    public static String extractFileNameFromUr1(String imageUr1l) throws MalformedURLException {
        URL url = new URL(imageUr1l);
        String path = url.getPath();
        return path.substring(path.lastIndexOf( '/') + 1);
    }



} //class End
